import entity.User;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");

        //加载驱动 alt+enter
        try {
            // 读取配置
            InputStream resourceAsStream = Main.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            // 加载流
            properties.load(resourceAsStream);
//            Class.forName("com.mysql.jdbc.Driver");
            Class.forName(properties.getProperty("db.driver"));
            // 获取连接
            Connection connection = DriverManager.getConnection(properties.getProperty("db.url"), properties.getProperty("db.user"), properties.getProperty("db.password"));
            // 获取操作数据工具
            PreparedStatement preparedStatement = connection.prepareStatement("select * from user");
            // 执行sql
            ResultSet resultSet = preparedStatement.executeQuery();
            //解析结果集
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getLong("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                System.out.println(user);
            }
            // 释放资源
            connection.close();
            preparedStatement.close();
            resultSet.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}